<?php

class NewsController extends AppController {

    public function index() {
        $this->paginate = array(
            'limit' => 10,
            'order' => array(
                'News.timestamp' => 'desc',
                'News.id' => 'desc'
            )
        );
        $this->set("data", $this->paginate('News'));
    }

    public function show($id, $title) {
        $result = $this->News->findById($id);
        $this->title = $result['News']['title'];
        $this->set("data", $result['News']);
    }

    public function admin_index() {
        $this->paginate = array(
            'limit' => 10,
            'order' => array(
                'News.timestamp' => 'desc',
                'News.id' => 'desc'
            )
        );
        $this->set("data", $this->paginate('News'));
    }

    public function admin_create() {
        $this->title = "Nowy news";

        $this->helpers[] = "CKEditor";
        if (!empty($this->data)) {
            if ($this->News->save($this->data)) {
                $this->Session->setFlash('Zapisano newsa.');
                $this->redirect(array('action' => 'index'));
            }
        }
    }

    public function admin_edit($id) {
        $this->helpers[] = "CKEditor";
        $this->title = "Edycja newsa";

        $this->News->id = $id;
        if (empty($this->data)) {
            $this->data = $this->News->read();
        } else {
            if ($this->News->save($this->data)) {
                $this->Session->setFlash('Zapisano newsa.');
                $this->redirect(array('action' => 'edit', $id));
            }
        }
    }

    public function admin_view($id) {
        $result = $this->News->findById($id);
        $this->title = "Edycja: " . $result['News']['title'];
        $this->set("data", $result['News']);
    }

}
?>
